package org.changs.game.android.ui.splash;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.CrashUtils;

import org.changs.game.android.MvpApp;
import org.changs.game.android.R;
import org.changs.game.android.ui.base.BaseActivity;
import org.changs.game.android.ui.main.MainActivity;

import javax.inject.Inject;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;

public class SplashActivity extends BaseActivity {

    public static final int REQUEST_CODE = 10001;

    @Inject CompositeDisposable disposable;
//    private Disposable setupSubscribe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_layout);
        setUnBinder(ButterKnife.bind(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkPermissions();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposable.dispose();
    }

    @SuppressLint("MissingPermission")
    @Override
    protected void setUp() {
        CrashUtils.init("/sdcard/educate-crash");
        ((MvpApp) getApplication()).injectIfNecessary();
        getActivityComponent().inject(this);

//        if (setupSubscribe != null && !setupSubscribe.isDisposed()) {
//            return;
//        }
//        setupSubscribe = Observable.timer(2, TimeUnit.SECONDS)
//                .observeOn(AndroidSchedulers.mainThread())
//                .doOnNext(new Consumer<Long>() {
//                    @Override
//                    public void accept(Long aLong) throws Exception {
//                        tvMessage.setText("正在初始化语音引擎...");
//                    }
//                })
//                .observeOn(Schedulers.io())
//                .map(new Function<Long, Boolean>() {
//                    @Override
//                    public Boolean apply(Long aLong) throws Exception {
//                        return turingManager.init();
//                    }
//                })
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<Boolean>() {
//                    @Override
//                    public void accept(Boolean aBoolean) throws Exception {
//                        startActivity(MainActivity.getStartIntent(SplashActivity.this));
//                        finish();
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        startActivity(MainActivity.getStartIntent(SplashActivity.this));
//                        finish();
//                    }
//                });

//        disposable.add(setupSubscribe);

        startActivity(MainActivity.getStartIntent(SplashActivity.this));
    }

    private void checkPermissions() {
        boolean granted = requestPermissionsSafely(new String[]{
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA,
        }, REQUEST_CODE);

        if (granted) {
            setUp();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            boolean granted = true;
            if (grantResults.length == 0) {
                granted = false;
            } else {
                for (int grantResult : grantResults) {
                    if (grantResult != PackageManager.PERMISSION_GRANTED) {
                        granted = false;
                        break;
                    }
                }
            }
            if (granted) {
                setUp();
            } else {
                checkPermissions();
            }
        }
    }
}
